package book.book.dao;

import book.book.bean.Books;
import book.book.util.Base;
import book.book.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class BooksDaoImpl extends Base implements BooksDao{

    @Override
    public int insertBook(Books books) {
        String  sql = "insert into tb_books values(?,?,?,?,?,?)";
       return update(sql,books.getBookid(),books.getBookname(),books.getPublisher(),
                books.getAuthor(),books.getBooktype(),books.getRemain());
    }

    @Override
    public int deleteBook(String bookid) {
        String  sql = "delete from tb_books where bookid=?";
        return update(sql,bookid);

    }


    @Override
    public int updateBook(Books books) {
        String  sql = "update tb_books set bookname=?,publisher=?,author=?,booktype=?,remain=?" +
                " where bookid=?";
        return update(sql,books.getBookname(),books.getPublisher(),books.getAuthor(),
                books.getBooktype(),books.getRemain(),books.getBookid());

    }

    @Override
    public List<Books> selectallBooks() {
        String  sql = "select * from tb_books";
        return getAll(Books.class, sql);
    }


    @Override
    public List<Books> selectBookLikeName(String name) {
        String  sql = "select * from tb_books where bookname like ?";
        List<Books> all = getAll(Books.class, sql, "%" + name + "%");
        return all;
    }

    @Override
    public Books queryBookByName(String bookname, String author) {
        String sql = "select * from tb_books where bookname=? and author=?";
        return getOne(Books.class, sql, bookname, author);
    }

    @Override
    public Books selectBookByBookId(String bookId) {

        String  sql = "select * from tb_books where bookid=?";
        return getOne(Books.class, sql, bookId);
    }
}
